<?php
function getDirs($sciezka){     
    $dirs = array();
    if ( !($dp = opendir($sciezka)) ) {
        die ( "Blad" );
    }
    while( $file = readdir($dp) ) {
        if(is_dir($file) && $file != '.' && $file != '..' && substr($file, 0 , 1) != '.' ) {
            $dirs[] = $file.'/';    
        } 
    }
    return $dirs;
}

function getFiles($sciezka){     
    $files = array();
    if ( !($dp = opendir($sciezka)) ) {
        die ( "Blad" );
    }
    while( $file = readdir($dp) ) {
        if($file != '.' && $file != '..' && substr($file, 0 , 1) != '.' ) {
            $files[] = $file;    
        } 
    }
    return $files;
}

function getGroups($path, $lang){   
	$groups = array();

    if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml -> grupa as $group ) {

        	foreach( $group->nazwa as $nazwa ) {     
	            if( $nazwa["lang"] == $lang) {
		        	$groupId = $group["id"];
	                $groupName = $nazwa;
	                $groups["$groupId"] = $groupName;
	            }
        	}
        }
    }    
    return $groups;
}

class Group {};
function getGroup($path, $gid, $lang){   
    $groups = array();
    $gr;
    if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml -> grupa as $group ) {
        	if($group["id"] == $gid) { 
        		foreach( $group->desc as $desc ) {     
	                if( $desc["lang"] == $lang) {
	                    $gr = $group;
	                }
	            }
        	}
        }
    }    
    return $gr;
}

function getBeginning($path, $lang){   
    $groups = array();
    $beginning = "";
    if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml->text as $text ) { 
            if( $text["lang"] == $lang) {
         //   	echo $text["lang"]." ".$lang." ".$text;
                $beginning = $text;
            }
        }
    }   
    return $beginning;
}

class Place { }

function getPlaces($path){     
	$places = array();
    if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml -> placowka as $place ) {
            $p = new Place();
            $p->name = $place->nazwa;
            $p->address = $place->adres;
            $p->city = $place->miasto;
            $p->zipcode = $place->kod;
            $p->fullAddress =  $place->miasto.', '.$place->adres;
            foreach( $place -> telefon as $phone ) {
                $p->phones[] = $phone;
            }
            foreach( $place -> fax as $fax ) {
                $p->faxes[] = $fax;
            }
            foreach( $place -> email as $email ) {
                $p->emails[] = $email;
            }
            foreach( $place -> gg as $gg ) {
                $p->ggs[] = $gg;
            }                        
            $places[] = $p;
        }
    }    
    return $places;
}


function getCurrentPath() {
	$path = $_SERVER['SCRIPT_NAME'];
    $queryString = "";//$_SERVER['QUERY_STRING'];
    
    return "/".$path.$queryString;

}
function getCurrentMenuPath() {
	$path = $_SERVER['SCRIPT_NAME'];

    $path = substr($path, 0, strrpos($path,"/") );

    return $path;

}

class MenuItem {}

function getMenuItems($path, $lang) {
	$items = array();
	
    if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml -> item as $item ) {
        
            $mi = new MenuItem();  
            foreach( $item->label as $label ) {   
                if( $label["lang"] == $lang) {
                    $mi -> title = $label;
                    $mi -> path = $item -> path;
                    $items[] = $mi;
                }
            }
        }
    }    
    return $items;
}

function getLeftMenuItems($path, $lang) {
    $items = array();
    if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml -> item as $item ) {
            $mi = new MenuItem();  
            foreach( $item->label as $label ) {   
                $mi -> id = $item->id;
                if( $label["lang"] == $lang) {
                    $mi -> title = $label;
		            foreach( $item->path as $path ) {   
		                if( $path["lang"] == $lang) {
		                    $mi -> path = $path;
				            $items[] = $mi;       
		                    break;
		                }
		            }     
                    break;
                }
            }

        }
    }    
    return $items;
}

class MapItem { }
function getMapItems($path, $lang) {
	$items = array();
	if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml -> item as $item ) {
        	$mi = new MapItem(); 
        	$mi->path = $item->path;
            foreach( $item->label as $label ) {   
                if( $label["lang"] == $lang) {
                    $mi -> label = $label; 
                    break;
                }
            }
            foreach( $item->label as $label ) {   
                if( $label["lang"] == $lang) {
                    $mi -> label = $label; 
                    break;
                }
            }
            
            foreach ($item->subitems as $subitems) {  
            	
	            foreach ($subitems->item as $subitem) {
	            	$submi = new MapItem(); 
	            	$submi -> path = $subitem -> path;
		            foreach( $subitem->label as $label ) {   
		                if( $label["lang"] == $lang) {
		                    $submi -> label = $label; 
		                    break;
		                }
		            }
		            $mi->subitems[] = $submi;
	            }
	            
            }            
            $items[] = $mi; 
        }        
	}
	return $items;
}


function cut($str, $count) {	
	if (strlen($str) > $count) {
		$str = substr($str, 0, $count).'...';
	}
    return $str;
}

class Banner {}
function getBanners($path, $lang){   
	$banners = array();

    if (file_exists($path)) {
        $xml = simplexml_load_file($path);
        foreach( $xml -> baner as $banner ) {
            $b = new Banner();
		    $b->foto = $banner->foto;
		    $b->url = $banner->url;
		    $banners[] = $b;
        }
    }    
    return $banners;
}
?>